package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.MchntApexInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntApexInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

public class MchntApexInfoMapperImpl implements MchntApexInfoMapper {

    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntApexInfo(MchntApexInfo mchntApexInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntApexInfo");
        if (StringUtil.isEmptyOrNull(mchntApexInfo.getExpiryDate())) {
            mchntApexInfo.setExpiryDate(BompAppParamConstant.DEFAULT_EXPIRY_DATE);
        }
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntApexInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户拓展属性信息失败,instId:<{}>,mchntNo:<{}>,propertyKey:<{}>", mchntApexInfo.getInstId(), mchntApexInfo.getMchntNo(), mchntApexInfo.getPropertyKey());
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntApexInfoByPrimaryKey(MchntApexInfo mchntApexInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntApexInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntApexInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键更新商户拓展属性信息失败,instId:<{}>,mchntNo:<{}>,propertyKey:<{}>", mchntApexInfo.getInstId(), mchntApexInfo.getMchntNo(), mchntApexInfo.getPropertyKey());
        }
        return iReturnCode;
    }

    @Override
    public MchntApexInfo selectMchntApexInfoByPrimaryKey(String instId, String mchntNo, String propertyKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApexInfoByPrimaryKey");
        MchntApexInfo mchntApexInfo = new MchntApexInfo();
        mchntApexInfo.setInstId(instId);
        mchntApexInfo.setMchntNo(mchntNo);
        mchntApexInfo.setPropertyKey(propertyKey);
        mchntApexInfo.setEffectiveDate(DateUtil.getCurrentDate());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApexInfo, mchntApexInfo);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键获取商户扩展属性信息失败,instId:<{}>,mchntNo:<{}>,propertyKey:<{}>", instId, mchntNo, propertyKey);
            return null;
        }
        return mchntApexInfo;
    }

    @Override
    public MchntApexInfo selectMchntApexInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApexInfoById");
        MchntApexInfo mchntApexInfo = new MchntApexInfo();
        mchntApexInfo.setInstId(instId);
        mchntApexInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntApexInfo, mchntApexInfo);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键获取商户扩展属性信息失败,instId:<{}>,id:<{}>", instId, id);
            return null;
        }
        return mchntApexInfo;
    }

    @Override
    public List<MchntApexInfo> selectMchntApexInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntApexInfoByMchntNo");
        MchntApexInfo mchntApexInfo = new MchntApexInfo();
        mchntApexInfo.setInstId(instId);
        mchntApexInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntApexInfo, MchntApexInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号查询商户扩展属性信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntApexInfo) item).collect(Collectors.toList());
    }
}
